from django.db import models


class Question(models.Model):
    """投票管理"""
    FLAG = (
        (1, '有效'),
        (0, '无效'),
    )
    CATEGORY = (
        (0, '图文'),
        (1, '文字'),
    )
    name = models.CharField(verbose_name="投票题目", max_length=200, blank=True, null=True)
    desc = models.CharField(verbose_name='说明', max_length=600, blank=True, null=True)
    begin_date = models.DateField(verbose_name="开始时间", blank=True, null=True)
    end_date = models.DateField(verbose_name="结束时间", blank=True, null=True)
    max_num = models.IntegerField(verbose_name="最大数量", blank=True, default=0)
    category = models.IntegerField(verbose_name="类型", default=0, blank=True, choices=CATEGORY)
    flag = models.IntegerField(verbose_name="状态", blank=True, choices=FLAG, default=1)
    update_time = models.DateTimeField(verbose_name="更新时间", auto_now=True)
    add_time = models.DateTimeField(verbose_name="增加时间", auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "投票管理"
        verbose_name_plural = verbose_name
        ordering = ["-id"]


class Choice(models.Model):
    """投票选项"""
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name="choices", blank=True, null=True, db_constraint=False)
    name = models.CharField(verbose_name="选项", max_length=300, blank=True, null=True)
    vote_nums = models.IntegerField(verbose_name="投票数量", blank=True, default=0)
    update_time = models.DateTimeField(verbose_name="更新时间", auto_now=True)
    add_time = models.DateTimeField(verbose_name="增加时间", auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "投票选项"
        verbose_name_plural = verbose_name


class ChoiceImage(models.Model):
    """投票选项图片"""
    choice = models.ForeignKey(Choice, on_delete=models.CASCADE, related_name="images", blank=True, null=True, db_constraint=False)
    image = models.ImageField(verbose_name="图片地址", blank=True, null=True)
    update_time = models.DateTimeField(verbose_name="更新时间", auto_now=True)
    add_time = models.DateTimeField(verbose_name="增加时间", auto_now_add=True)

    def __str__(self):
        return self.choice.name if self.choice else "无"

    class Meta:
        verbose_name = "投票选项图片"
        verbose_name_plural = verbose_name


class VoteDetail(models.Model):
    """投票详情"""
    openid = models.CharField(verbose_name='微信II', max_length=32, blank=True, null=True)
    nickname = models.CharField(verbose_name='昵称', max_length=32, blank=True, null=True)
    question = models.ForeignKey(Question, verbose_name="投票题目", on_delete=models.CASCADE, blank=True, null=True, db_constraint=False)
    choice_name = models.CharField(verbose_name="投票选项", max_length=1200, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name="更新时间", auto_now=True)
    add_time = models.DateTimeField(verbose_name="增加时间", auto_now_add=True)

    def __str__(self):
        return self.openid

    class Meta:
        verbose_name = "投票详情"
        verbose_name_plural = verbose_name
        ordering = ["-id"]
